﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_County")]
   public class P_County:IEntity<P_County>
    {
        [Key]
        [Column(TypeName = "varchar")]
        [StringLength(10)]
        public string CountyCode { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(50)]
        public string CountyName { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(10)]
        public string CityCode { get; set; }

        public override P_County Find(string key)
        { 
            return Find(c => c.CountyCode == key);
        }

        public override P_County Find(System.Linq.Expressions.Expression<Func<P_County, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.County.FirstOrDefault(where);
                if (item != null)
                {
                    this.CountyCode = item.CountyCode;
                    this.CityCode = item.CityCode;
                    this.CountyName = item.CountyName;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_County, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.County.Any(where);
            }
        }
    }
}
